package com.java.comprehensive.arithmetic.leecode.easy;

import util.ListNode;

/**
 * @program: comprehensive-service
 * @description: 链表反转
 * @author: 窦绍飞
 * @create: 2021-12-15 11:14
 **/
public class LeetCode_206 {
    public static ListNode reverseList(ListNode head) {

        ListNode dummny = new ListNode(0);
        dummny.next = head;
        while (head != null && head.next != null) {
            ListNode dummnyNext = dummny.next;
            ListNode temp = head.next;
            dummny.next = temp;
            head.next = temp.next;
            temp.next = dummnyNext;
        }
        return dummny.next;
    }

    public static ListNode reverseListTwo(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        while (head != null && head.next != null) {
            ListNode listNode = dummy.next;
            ListNode temp = head.next;
            dummy.next = temp;
            head.next = temp.next;
            temp.next = listNode;
        }
        return dummy.next;
    }

    public ListNode reverseListTowPoints(ListNode head) {
        ListNode curr = head;
        ListNode pre = null;
        while (curr != null) {
            ListNode temp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = temp;
        }
        return pre;
    }

    public ListNode reversal(ListNode curr, ListNode pre) {
        if (curr == null) return pre;
        ListNode temp = curr.next;
        curr.next = pre;
        return reversal(temp, curr);
    }

    public static void main(String[] args) {
        ListNode listNode3 = new ListNode(5, null);
        ListNode listNode4 = new ListNode(4, listNode3);
        ListNode listNode5 = new ListNode(3, listNode4);
        ListNode listNode6 = new ListNode(2, listNode5);
        ListNode listNode7 = new ListNode(1, listNode6);
        ListNode listNode = reverseListTwo(listNode7);
        System.out.println(listNode);
    }

}
